<?php
class Events extends Controller {
    private $imgdata = array(
        'dir' => array(
        'original' => 'images/events/original/',
        'thumb' => 'images/events/thumb/',
        'large'=>'images/events/large/'
    ),
    'total' => 0,
    'images' => array(),
    'error' => ''
    );
private $imgerror;



        public function saveevent()
    {

         
        $this->load->model('event_model');
        $this->event_model->saveevents();
    }
    
    
    public function eventlist() {
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Events',"#");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();  
        $this->load->model('event_model');
        $this->load->library("pagination");
        $config = array();
        $config["base_url"] = base_url() ."secu/event/eventlist/";
        $config["total_rows"] = $this->event_model->get_event_list_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 4;
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        $data["pagination"] = $this->pagination->create_links();
        $acclist=$this->event_model->get_event_list($config["per_page"], $page);
        $data["acclist"]=$acclist;
        $this->template->set('controller', $this);
        $this->template->set('title', "login");
        $this->template->load_partial('admintemplate','event/eventlist',$data);

    }

   public function addevent() {
        $data["partnerlang"]=$this->config->item("partnerlang");
       // $this->template->load('admintemplate','admin/event',$data);
         $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Event',base_url()."secu/events/eventlist");
        $this->breadcrumb->addCrumb('New Event',"#");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();  
        $this->template->set('controller', $this);
        $this->template->load_partial('admintemplate','event/addevent',$data);
    }

    public function doaddevent()
    {
        $partnerlang = $this->config->item("partnerlang");
        $this->load->model("event_model");
        $validate_arr_lang=array();
        $event_detail_arr_lang=array();
        foreach($partnerlang as $langkey=>$langval){
            $event_detail_arr_lang+=array("title".$langkey=>$this->input->post('title'.$langkey),
                                         "desc".$langkey=>$this->input->post('desc'.$langkey));
        }
          $eventstartdate=$this->input->post("event_start");
          $status=$this->input->post("status");
          $eventdetails=array();
          $eventdetails["event_start"]=$eventstartdate;
          $eventdetails["status"]=$status;
          $eventdetails["event_detail_arr_lang"]=$event_detail_arr_lang;
          $this->event_model->addnewevent($eventdetails,$partnerlang);
          redirect('secu/events/eventlist');

    }
    /*public function eventlist() {
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->model('event');
        $acclist=$this->event->get_event_list();
        $data["acclist"]=$acclist;
        $this->template->load('admintemplate','admin/eventlist',$data);
    }*/

    public function eventsedit()
    {
        $eventid= $this->uri->segment(5);
    //    echo "****".$accid;exit;
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->model('event_model');
        $eventdetails=$this->event_model->get_event_details($eventid);
       // print_r($eventdetails);exit;
        $data["eventdetails"]=$eventdetails;
         $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Event',base_url()."secu/events/eventlist");
        $this->breadcrumb->addCrumb('Edit Event',"#");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();  
        $this->template->load('admintemplate','secu/event/eventedit',$data);
    }

    public function doeditevent()
    {
              $data["partnerlang"]=$this->config->item("partnerlang");
              $eventdetails=array();
              //$this->template->set('product_header', 'Admin Home');
              $this->load->model('event_model');
              $this->event_model->doeditevent($_POST,$data["partnerlang"]);
              redirect('secu/events/eventlist');
              //$this->load->model('user_security');

    }

     public function eventsdelete()
    {

              //$this->template->set('product_header', 'Admin Home');
               $accid= $this->uri->segment(5);
                $data["partnerlang"]=$this->config->item("partnerlang");
              $this->load->model('event_model');
              $this->event_model->deleteevent($accid, $data["partnerlang"]);
              redirect('secu/events/eventlist');
              //$this->load->model('user_security');
      }
      
    /*public function featuredeventlist() {
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->model('event_model');
        $featuredacclist=$this->event_model->get_featuredevent_list();
        $data["featuredacclist"]=$featuredacclist;
        $this->template->set('controller', $this);
        $this->template->set('title', "login");
        $this->template->load_partial('admintemplate','event/eventlist',$data);

    }*/
    
    public function addfeaturedevent() {
        $this->load->model('event_model');
        $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Events',"#");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();  
        $data["partnerlang"]=$this->config->item("partnerlang");
        $eventlist=$this->event_model->get_event_list_for_featuredevents();
        $feturedevent=$this->event_model->get_featuredevent_for_featuredevents();
        //print_r();exi
       // $this->template->load('admintemplate','admin/event',$data);
        $data["feturedevent"]=$feturedevent;
        $data["eventlist"]=$eventlist;
        $this->template->set('controller', $this);
        $this->template->load_partial('admintemplate','event/featured_event',$data);
    }
   
    public function updatefeaturedevent()
    {
          // print_r($_POST);exit;
           $this->load->model('event_model');
           $eventid=$this->input->post("eventid");
           $this->event_model->updatefeaturedevent($eventid);
         redirect('secu/events/addfeaturedevent/');
    }
    


     public function savefeaturedeventimages()
     {
        $id=$this->input->post("id");
        $c_upload['upload_path']    = $this->imgdata['dir']['original'];
        $c_upload['allowed_types']  = 'gif|jpg|png|jpeg|x-png';
        $c_upload['max_size']       = '10000';
        $c_upload['max_width']      = '10000';
        $c_upload['max_height']     = '5000';
        $c_upload['remove_spaces']  = TRUE;
        //print_r($c_upload);
        
        $this->load->library('upload', $c_upload);

        if ($this->upload->do_upload()) {
//
            $img = $this->upload->data();
            $new_image = $this->imgdata['dir']['original'].trim($id)."_".trim(time())."_".$img['file_name'];
            $this->img_resize($img, $new_image,$id);
            
            unlink($this->imgdata['dir']['original'].$img['file_name']);
            //exit($this->imgdata['dir']['original'].$img['file_name']);
            $this->load->model('event_model');
            //exit();
            $this->event_model->updatefeaturedeventimage($id,trim($id)."_".trim(time())."_".$img['file_name']);



        } else {
            $this->imgerror= $this->upload->display_errors();            
        }
        redirect('secu/events/eventsedit/id/'.$id);
    }
    
    public function img_resize($upload,$new_image,$id)
    {
        $this->load->library('image_lib');
        $newpath = $new_image;

        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 595;
        $config['height']    = 320;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $newpath_thumb = $this->imgdata['dir']['thumb']."thumb_".trim($id)."_".trim(time())."_".$upload['file_name'];

        unset($config);
        $this->image_lib->clear();
         $this->make_thumb($upload['full_path'],$newpath_thumb);
    }
    
    public function make_thumb($tempimage,$path)
    {

        $config['image_library'] = 'gd2';
        $config['source_image']    = $tempimage;
        $config['new_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 126;
        $config['height']    = 129;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
    }
    
    public function deletefeaturedaccimage()
    {
        $original_status=false;
        $thumb_status=false;
        $del_image_status="failed";
        $id=$this->uri->segment(7);
        $ori_img=$this->uri->segment(5); 
        $ext=  substr($ori_img, -3);        
        $ori_img=  substr($ori_img, 0, strlen($ori_img)-4).'.'.$ext;
        $this->load->model('content');
        $this->content->deleteimage($id, "featuredacc");
        //
        //echo $this->imgdata['dir']['original'].$ori_img.'<br>';
        if (file_exists($this->imgdata['dir']['original'].$ori_img)){
            unlink($this->imgdata['dir']['original'].$ori_img);
            $original_status=true;
        }
        //echo $this->imgdata['dir']['thumb'].'thumb_'.$ori_img;
        if (file_exists($this->imgdata['dir']['thumb'].'thumb_'.$ori_img)){
            unlink($this->imgdata['dir']['thumb'].'thumb_'.$ori_img);
            $thumb_status=true;
        }
        //exit();
        if ($original_status && $thumb_status){
            $del_image_status="successfully";
        }elseif ($original_status || $thumb_status) {
            $del_image_status="partially";
        }
        redirect('secu/event/featuredeventedit/id/'.$id.'/'.$ori_img.'/'.$del_image_status);
    }
}
?>
